package com.chinazyjr.framework.common.utils.fenPei;


import com.chinazyjr.framework.common.model.autoModel.InvesterVo;
import com.chinazyjr.framework.common.model.autoModel.ProjectVo;
import org.apache.commons.collections.map.HashedMap;
import org.slf4j.LoggerFactory;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class FenPeiUtil {
    private static final org.slf4j.Logger LOGGER = LoggerFactory.getLogger(FenPeiUtil.class);

    /**
     * 按优先级和份数list  来分配balance  得出分配后的list和剩余的balance
     * @param incomeList
     * @param balance
     * @return
     */
    public static Map<String,Object> getResultList(List<BigDecimal> incomeList, BigDecimal balance){
        Map<String,Object> resultMap= new HashedMap();
        List<BigDecimal> resultList = new ArrayList();
        for (int i=0;i<incomeList.size();i++){
            resultList.add(BigDecimal.ZERO);
        }
        for(int j=0;j<incomeList.size(); j++){
            if(balance==null ||incomeList.isEmpty()){
                continue;
            }
            BigDecimal income = incomeList.get(j)==null?BigDecimal.ZERO:incomeList.get(j);
            if(balance.compareTo(income)>0){
                resultList.set(j,incomeList.get(j));
                balance= MoneyUtil.AmountSub(balance,incomeList.get(j));
            }else if(balance.compareTo(income)<0){
                resultList.set(j,balance);
                balance = BigDecimal.ZERO;
                break;
            }else if(balance.compareTo(income)==0){
                resultList.set(j,incomeList.get(j));
                balance = BigDecimal.ZERO;
                break;
            }
        }
        resultMap.put("list",resultList );
        resultMap.put("balance",balance );
        return resultMap;
    }


    public static BigDecimal sigleAuto(ProjectVo projectVo, InvesterVo investerVo){
        BigDecimal fenDao = BigDecimal.ZERO;

        //初始化参数
        Map<Integer, Integer> limitDays = investerVo.getLimitDays();
        Integer min = 0;
        Integer max = 10000;
        BigDecimal maxMoney = investerVo.getMaxMoney();
        BigDecimal minMoney = investerVo.getMinMoney();
        Byte myProType = investerVo.getMyProType();
        BigDecimal balanceMoney = investerVo.getBalanceMoney();

        Integer limitDay = projectVo.getLimitDay();
        Byte proType = projectVo.getProType();
        BigDecimal leftMoney = projectVo.getLeftMoney();

        //判断是否匹配
        for (Map.Entry<Integer, Integer> entry : limitDays.entrySet()) {
            min=entry.getKey();
            max= entry.getValue();
        }
        //标的期限
        if(limitDay<min || limitDay>max){
            LOGGER.error("标的期限不匹配");
            return fenDao;
        }
        //标的类型
        if(!myProType.equals(proType)){
            LOGGER.error("标的类型不匹配");
            return fenDao;
        }

        //得到fenDao
        if(balanceMoney.compareTo(leftMoney)<=0){
            fenDao=balanceMoney;
        }else if(balanceMoney.compareTo(leftMoney)>0){
            fenDao=leftMoney;
        }

        if(fenDao.compareTo(maxMoney)>0){
            fenDao=maxMoney;
        }
        return fenDao;
    }


}


